package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.Encrypt;
import com.example.lotterysystem.dao.dataobject.UserDO;
import jakarta.validation.constraints.NotBlank;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {


    //查询邮箱绑定人数
    @Select("select count(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);
    //查询手机号绑定人数
    @Select("select count(*) from user where phone_number= #{phone}")
    int countByPhone(Encrypt encrypt);

    //插入用户信息
    @Insert("insert into user(user_name,email,phone_number,password,identity) " +
            "values(#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true, keyProperty = "id",keyColumn = "id")
    void insert(UserDO userDO);

    // package com.example.lotterysystem.dao.mapper;

    @Select("select * from user where email = #{email}")
    UserDO selectByMail(@NotBlank(message = "手机或邮箱不能为空！") @Param("email") String email);

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 这里 identity 可能会为空，如果传入空，那么这个 select 会去表中查询 identity 为空的数据
     * 为了避免这种情况，需要在 SQL 语句中加一个判断 if，写成动态 SQL（注意：其中的 " 需要转义）
     * 还需要用 <script></script> 将其包起来，说明其是一个脚本来写的 SQL
     * @param identity
     * @return
     */
    @Select("<script> " +
            " select * from user " +
            " <if test=\"identity!=null\"> " +
            " where identity = #{identity} " +
            " </if> " +
            " order by id desc " +
            " </script>")
    List<UserDO> findUserInfo(@Param("identity") String identity);

    /**
     *  批量查询人员信息
     * @param ids
     * @return
     */
    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> ids);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);
}
